define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'douyin_shop/index' + location.search,
                    add_url: 'douyin_shop/add',
                    edit_url: 'douyin_shop/edit',
                    del_url: 'douyin_shop/del',
                    multi_url: 'douyin_shop/multi',
                    import_url: 'douyin_shop/import',
                    table: 'douyin_shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'auth.name', title: __('Auth.name'), operate: 'LIKE'},
                        // {field: 'meituan_auth_id', title: __('Meituan_auth_id')},
                        {field: 'shop_name', title: __('Shop_name'), operate: 'LIKE'},
                        {field: 'city_name', title: __('City_name'), operate: 'LIKE'},
                        {field: 'shop_address', title: __('Shop_address'), operate: 'LIKE'},
                        {field: 'open_shop_uuid', title: __('Open_shop_uuid'), operate: 'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},

                        // {field: 'auth.id', title: __('Auth.id')},
                        // {field: 'auth.name', title: __('Auth.name'), operate: 'LIKE'},
                        // {field: 'auth.app_key', title: __('Auth.app_key'), operate: 'LIKE'},
                        // {field: 'auth.app_secret', title: __('Auth.app_secret'), operate: 'LIKE'},
                        // {field: 'auth.bid', title: __('Auth.bid'), operate: 'LIKE'},
                        // {field: 'auth.token_type', title: __('Auth.token_type'), operate: 'LIKE'},
                        // {field: 'auth.scope', title: __('Auth.scope'), operate: 'LIKE'},
                        // {field: 'auth.session', title: __('Auth.session'), operate: 'LIKE'},
                        // {field: 'auth.session_expire_time', title: __('Auth.session_expire_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'auth.session_status', title: __('Auth.session_status')},
                        // {field: 'auth.session_check_time', title: __('Auth.session_check_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'auth.refresh_session', title: __('Auth.refresh_session'), operate: 'LIKE'},
                        // {field: 'auth.remain_refresh_count', title: __('Auth.remain_refresh_count')},
                        // {field: 'auth.last_refresh_time', title: __('Auth.last_refresh_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'auth.log_info', title: __('Auth.log_info'), operate: 'LIKE'},
                        // {field: 'auth.create_time', title: __('Auth.create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'auth.update_time', title: __('Auth.update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                        //         var html = [];
                        //         html.push('<a href="javascript:;" class="btn btn-xs btn-success btn-editone" title="编辑" data-id="' + row.id + '"><i class="fa fa-pencil"></i></a>');
                        //         html.push('<a href="javascript:;" class="btn btn-xs btn-danger btn-delone" title="删除" data-id="' + row.id + '"><i class="fa fa-trash"></i></a>');
                        //
                        //         html.push('<a href="javascript:;" class="btn btn-xs btn-info btn-dy-ajax-url btn-start-meituan-auth" title="授权" data-url="douyin_shop/start_auth"  data-id="' + row.id + '">准备</a>');
                        //         // html.push('<a href="javascript:;" class="btn btn-xs btn-info btn-dy-ajax-url btn-start-meituan-auth" title="授权" data-url="douyin_shop/verify_coupon"  data-id="' + row.id + '">验券</a>');
                        //         html.push('<a href="javascript:;" class="btn btn-xs btn-info btn-dy-ajax-url btn-start-meituan-auth" title="授权" data-url="douyin_shop/revoke"  data-id="' + row.id + '">撤销券</a>');
                        //         return html.join(' ');
                        //     }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-dy-ajax-url", function () {
                var url = $(this).data("url");
                var id = $(this).data("id");
                //ajax请求此url,并弹出提示
                $.ajax({
                    url: url+"?id="+id,
                    type: 'post',
                    dataType: 'json',
                    success: function (ret) {
                        if (ret.code == 1) {
                            Toastr.success(ret.msg);
                        } else {
                            Toastr.error(ret.msg);
                        }
                    },
                    error: function (e) {
                        Toastr.error("网络错误");
                    }
                });
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
